<template>
  <div style="line-height: 60px; display: flex">
    <div style="flex: 1; font-size: 20px">
      <span :class="collapseBtnClass" style="cursor: pointer" @click="collapse"></span>

      <el-breadcrumb separator="/" style="display: inline-block; margin-left: 10px">
        <el-breadcrumb-item v-for="item in lists" :key="item.path"> <!--遍历路由信息并展示 -->
          <router-link :to="item.path">{{item.meta.title}}</router-link>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-dropdown style="cursor: pointer">
      <div style="display: inline-block">
        <img :src="user.avatarUrl" alt="" style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
        <span>{{user.nickname != null ? user.nickname : "请前往个人中心设置昵称"}}</span>
        <i class="el-icon-arrow-down" style="margin-left: 5px"></i>
      </div>
      <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <router-link style="text-decoration: none; color: #606266" to="/person">个人信息</router-link>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <span style="text-decoration: none" @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
export default {
  name: "Header",
  props: {
    collapseBtnClass: String,
  },
  data() {
    return {
      lists: [],   //定义一个数组 用于接收路由信息
      user: localStorage.getItem("user") != null ? JSON.parse(localStorage.getItem("user")) : {},
    }
  },
  created() {
    this.lists = this.$route.matched  //获取路由内的全部信息
  },
  methods: {
    collapse() {
      this.$emit("asideCollapse");
    },
    logout() {
      this.$router.push("/login");
      localStorage.removeItem("user");
      this.$message.success("已退出登录");
    }
  },
  //这里必须使用监听，否则无法实时获取路由变动信息。
  //监听后路由会实时变动，不然需要手动刷新路径才会改变
  watch:{
    $route(to)  {
      this.lists = to.matched;
    }
  },

}
</script>

<style scoped>

</style>